home *** CD-ROM | disk | FTP | other *** search
- Path: news.mira.net.au!news
- From: davidw@werple.net.au (David White)
- Newsgroups: comp.lang.c++
- Subject: Re: int (*p)[4]; (Watcom problem)
- Date: 9 Apr 1996 20:28:07 +1000
- Organization: Werple Internet, Melbourne
- Message-ID: <4kde3n$6s8@werple.net.au>
- References: <4kcd03$37j@morgoth.sfu.ca>
- NNTP-Posting-Host: werplez.mira.net.au
- Keywords: WATCOM C++ POINTER ARRAY PROBLEM
-
- mpohores@news.sfu.ca (Michael John Pohoreski) writes:
-
- >I came across (ARM, page 97) the following way to define
- >a pointer to an array:
- > int (*p)[];
-
- >(Yes, I allready know I can just do: int *p)
- >So I tried it in a small C++ test program: p2a.cpp
-
- >#include <stdio.h>
-
- >void main( void )
- >{
- > int a[4] = { -13, 11, -7, 5 };
- > int (*x)[4]; // pointer to array
-
- > for (int i = 0; i < 4; i++ )
- > {
- > (int *)x = &a[i];
- > printf( "%d\n", (*x)[0] );
- > }
- >}
-
- >Now here's my problem:
- > Under Watcom 10.5, the above doesn't compile with wcl386
- >but it compiles under DJGPP 2.0, and Borland C++ 3.1 Anyone know why?
-
- > Is this a bug?
-
- It surprises me that any compiler would accept this. A 'pointer to an array'
- points to an array, not to the type of the array's elements. The types
- 'int *' and 'int (*)[4]' are completely different. In the first case,
- dereferencing the pointer yields an 'int', in the second an 'int [4]'.
- To use the pointer correctly, do the following:
-
- #include <stdio.h>
-
- int main( void )
- {
- int a[4] = { -13, 11, -7, 5 };
- int (*x)[4] = &a;
-
- for (int i = 0; i < 4; i++ )
- {
- printf( "%d\n", (*x)[i] );
- }
- return 0;
- }
-
- David White
- davidw@werple.mira.net.au
-